"""
Case Type   : bugfix
Case Name   : B库兼容模式下，建表含text、longtext、mediumtext、tinytext数据类型，DML操作成功
Create At   : 2025/6/11
Owner       : liu-tong-8848
Description :
    1、建表包含text、longtext、mediumtext、tinytext数据类型
    2、插入数据
    3、查询数据
    4、修改数据
    5、删除数据
    6、删除表
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:建表包含text、longtext、mediumtext、tinytext数据类型；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fix_100;create table t_fix_100(v1 text(40), v2 longtext(30), " \
                  + f"v3 mediumtext(20), v4 tinytext(10), v5 int);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)

        text = '----step2:插入数据；expect:成功----'
        self.log.info(text)
        sql_cmd = "insert into t_fix_100 values (substring(md5(random()::text), 1, 35), " \
                  + "substring(md5(random()::text), 1, 25), substring(md5(random()::text), 1, 20), " \
                  + "substring(md5(random()::text), 1, 15), generate_series(1, 10000));"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step3:查询数据；expect:成功----'
        self.log.info(text)
        sql_cmd = "select count(*) from t_fix_100 where v5 < 1000;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('999', sql_res, '执行失败' + text)

        text = '----step4:修改数据；expect:成功----'
        self.log.info(text)
        sql_cmd = "update t_fix_100 set v1 = 'test text' where v5 > 1000;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step5:删除数据；expect:成功----'
        self.log.info(text)
        sql_cmd = "delete from t_fix_100 where v5 > 2000;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step6:删除表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fix_100;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
